<!DOCTYPE html>
<?php
session_start();
$userloged=isset($_SESSION["uid"]);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($userloged){
            echo '<a href="adduser.php">添加</a> <a href="logout.php">退出</a>';
        }else{
            echo '<a href="login.php">登录</a> ';
        }
        ?>
        
        <br/>
        <form action="mysql001.php" method="get">
            <input type="text" name="k"/><input type="submit" value="搜索"/>
        </form>
        <table border="1px">
            <tr>
                
                <th>编号</th>
                <th></th>
                <th>用户名</th>
                <th>密码</th>
                <th>年龄</th>
                <th>性别</th>
                 <?php
                if($userloged){
                    echo ' <th></th>';
                }
                ?>
               
            </tr>
         <?php
        require 'common/mysqlCommon.php';  //将其他的php文件的代码引入当前页面
        $connect=getConnect();
        $key=isset($_GET["k"])?$_GET["k"]:"";
        $pageSize=3;
        $sqlstr1="select * from user_infos where username like '%${key}%' order by uid desc";
        $tatalCount=count(getDataSetBySQL($sqlstr1));
       //$tatalCount数据的总数量
        //intval 取整数
        $totalPageCount=intval($tatalCount/$pageSize)+($tatalCount%$pageSize>0?1:0);
        echo $totalPageCount;
        echo $tatalCount;
        //对分页参数的处理 分页的参数不能是字符，也不能是负数
        $param_i=isset($_GET["i"])?$_GET["i"]:0;
        $param_i=(int)$param_i;
        //设置分页页码的下限
        $param_i=$param_i<=1?1:$param_i;
        //设置页数页码的上限
        $param_i=$param_i>$totalPageCount?$totalPageCount:$param_i;
        $pageIndex=$param_i-1;
        
//        echo"pageIndex-->".$pageIndex;
       
       
       //$totalPageCount 得到分页的总数
        if($connect){
//            echo "数据库连接成功！";
            //mysqli_set_charset 设置连接使用的字符集
            mysqli_set_charset($connect, "utf-8");
            
            //mysqli_query 执行sql语句并获得执行结果
            $sqlstring=$sqlstr1." limit ".($pageIndex*$pageSize).",".$pageSize;
//            echo $sqlstring;
            $result=mysqli_query($connect, $sqlstring);
            //mysqli_fetch_array 获取结果集中某一行的记录
            $row=mysqli_fetch_array($result);
            while($row){
//            echo "<br/>\$row :".gettype($row);
//            foreach($row as $k=>$v){
//                echo "<br/>".$k."  ->  ".$v;
//            }
//            echo "<br/> uid:".$row["uid"]." username:".$row["username"]."upwd:".$row["upwd"]
//                    ." age:".$row["upwd"]." sex:".$row["sex"];
//            echo "<br/> uid:".$row[0]." username:".$row[1]."upwd:".$row[2]
//                    ." age:".$row[3]." sex:".$row[4];
                ?>
                <tr>
                    <td><?php echo $row["uid"]; ?></td>
                    <td><img src="<?php echo $row["pic"]; ?>" style="width:100px;height:100px" /></td>
                    <td><?php echo $row["username"]; ?></td>
                    <td><?php echo $row["upwd"]; ?></td>
                    <td><?php echo $row["sex"]; ?></td>
                    <td><?php echo $row["age"]; ?></td>
                     <?php
                    if($userloged){
                   ?>

                    <td>
                        <a href="edituser.php?id=<?php echo $row["uid"]; ?>">编辑</a>
                        <a href="deluser.php?id=<?php echo $row["uid"]; ?>">删除</a>
                    </td>
                  <?php    
                    }
                    ?>
                </tr>

            <?php
            $row=mysqli_fetch_array($result);
            }
            
        }else{
            echo "数据库连接失败";
        }
        //mysqli_close 释放数据库连接
        mysqli_close($connect);
        ?>
         </table>
        <hr/>
        <?php
 
       for($temp=1;$temp<=$totalPageCount;$temp++){
          echo " | <a href=\"mysql001.php?i=${temp}\" >第".$temp."页 </a>"; 
       }
        ?>
    </body>
</html>
